
import java.awt.Color;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;
import java.io.IOException;
import java.util.HashMap;

import javax.imageio.ImageIO;

public class ImageLoader {
	
	private static ImageLoader instance = null;
	private HashMap<String,Image> images;
	
	private ImageLoader() {
		
		images = new HashMap<String,Image>();
	}
	
	public static ImageLoader get() {
		if (instance == null) 
			instance = new ImageLoader();
		
		return instance;
	}
	
	public Image read(String path) {
		try {
			Image image = images.get(path);
			
			if (image == null) {
				image = ImageIO.read(getClass().getResource(path));
				images.put(path, image);
			}
			
			return image;
			
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	
	
//	public Image read(String path, final Color color) {
//		
//		ImageFilter filter = new RGBImageFilter() {
//			// the color we are looking for... Alpha bits are set to opaque
//			public int markerRGB = color.getRGB() | 0x000000FF;
//
//			public final int filterRGB(int x, int y, int rgb) {
//				if ((rgb | 0x000000FF) == markerRGB) {
//					// Mark the alpha bits as zero - transparent
//					return 0x00FFFFFF & rgb;
//				} else {
//					// nothing to do
//					return rgb;
//				}
//			}
//		}
//	;
		
//		Image image = read(path);
		
//		ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
//		return Toolkit.getDefaultToolkit().createImage(ip);

	}
	
}
